//index.js
//获取应用实例
const app = getApp()
const constant = require("../../constant.js")
const storage  = require("../../utils/storage.js")
const auth     = require("../../utils/auth.js")

Page({
  
  data: {
    redirect: '',
    qiniu: constant.resource.qiniu
  },

  onLoad: function (options) {
    
    var that = this;

    // debug or not
    if (options.debug) {
      wx.setEnableDebug({
        enableDebug: true
      })

      let pages = getCurrentPages();  // 获取当前页面栈
      let currPage = pages[pages.length - 1];

      

    }

    // set device info into storage
    try {
      var res = wx.getSystemInfoSync()
      wx.setStorageSync(storage.keys.platform, res.platform)
      wx.setStorageSync(storage.keys.system, res.system)
    } catch (e) {
      console.log(e)
    }
    // do auth here
    that.doAuth();
    
  },

  onShow: function (options) {
  },

  doAuth: function (options) {
    var that = this;
    // get unionid key from storgae
    try {
      var unionid = wx.getStorageSync(storage.keys.unionid)
      var mobile = wx.getStorageSync(storage.keys.mobile)
      if (unionid) {
        if(mobile) {
          that.setData({
            unionid: unionid
          })
          wx.request({
            url: constant.api(app.globalData.pord).url + '/is-student?unionid=' + unionid,
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              if (res.data.code == "0") {
                var isStudent = res.data.data.isStudent;

                if (isStudent) {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '../profile/profile'
                    })
                  }, 2000)
                } else {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: that.data.redirect ? that.data.redirect : '/pages/malls/malls-index/malls-index'
                    })
                  }, 2000)
                }
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 1000
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '请求数据失败',
                icon: 'none',
                duration: 1000
              })
            }
          })
        } else {
          app.globalData.needAuth = true;
          wx.redirectTo({
            url: that.data.redirect ? that.data.redirect : '/pages/malls/malls-index/malls-index'
          })
          //auth.authRequest(that)
        }
      } else {
        app.globalData.needAuth = true;
        wx.redirectTo({
          url: that.data.redirect ? that.data.redirect : '/pages/malls/malls-index/malls-index'
        })
        //auth.authRequest(that)
      }
    } catch (e) {
      // console.log(e)
      // console.log("no unionid in storgae start auth")
      app.globalData.needAuth = true;
      wx.redirectTo({
        url: that.data.redirect ? that.data.redirect : '/pages/malls/malls-index/malls-index'
      })
      //auth.authRequest(that)
    }
  },

  authRequest: function () {
    // save auth reidrect page into storgae
    var that = this;
    try {
      //wx.setStorageSync(storage.keys.authRedirect, that.data.redirect ? that.data.redirect : '../../pages/profile/profile')
      auth.getUserInfo({}, function (unionid) {
        that.setData({
          unionid: unionid
        })
        wx.setStorageSync(storage.keys.authRedirect, "")
        setTimeout(function () {
          wx.redirectTo({
            url: that.data.redirect ? that.data.redirect : '/pages/profile/profile'
          })
        }, 2000)
      }, function () {
        console.log("auth get userinfo faild");
        wx.redirectTo({
          url: '/pages/error-page/error-page',
        })
      })
    } catch (e) {
      console.log(e)
    }
  }
})